package ltd.sshine.websocket.WebSocketStruct;

import ltd.sshine.websocket.DO.Message;
import ltd.sshine.websocket.Util.MapUtil;
import ltd.sshine.websocket.Util.MessageUtil;
import org.apache.commons.lang3.RandomStringUtils;

import javax.websocket.Session;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;



public class WebSocket {

    public static final MessageUtil messageUtil;


   public   String uid;
   public   String username;

    static {
        messageUtil=new MessageUtil();
    }
   public   Session session;
    public static final   MapUtil mapUtil=new MapUtil();

    public static final ConcurrentHashMap<String, WebSocket> webSocketSet = new ConcurrentHashMap<>();


    public void onOpen(Session session){

        this.session=session;

        String uid= UUID.randomUUID().toString().replaceAll("-","");
        //生成匿名用户
        String rand = RandomStringUtils.randomAlphanumeric(5);
        username="用户"+rand;
        sendGroup(mapUtil.getJsonMap("login","已加入",1,"total",webSocketSet.size()+1,"username",username));
        webSocketSet.put(uid,this);
        this.uid=uid;
        //用户
//        Message msg =
//                new Message()
//                        .setType("login")
//                        .setData(mapUtil.getmap("total", webSocketSet.size(),"msg","连接成功"))
//                        .setCode(1)
//                        .setUsername(username);


        sendOne(mapUtil.getJsonMap("login","连接成功",1,"total",webSocketSet.size(),"username",username));

    }
    //关

    public void onClose(){

        webSocketSet.remove(uid);
        sendGroup(mapUtil.getJsonMap("logout","已退出",1,"total",webSocketSet.size(),"username",username));
    }


    public  void onMessage(String message){

        Message msg=messageUtil.getMsgObj(message);


        if (msg==null){
            sendOne(mapUtil.getJsonMap("err","无法处理的json消息",0));
            return;
        }

        if (msg.getType()==null){

            sendOne(mapUtil.getJsonMap("err","发送的数据未指定类型",0));
            return;

        }
        if ("hbt".equals(msg.getType())){

            sendOne(mapUtil.getJsonMap("hbt","",1));
            return;
        }
        if ("changeName".equals(msg.getType())){
            String name = msg.getUsername();
            if (name==null){

                sendOne( mapUtil.getJsonMap("err","没有发送用户名",0));

            }else if("".equals(name.trim())){

                sendOne( mapUtil.getJsonMap("err","用户名不能为空",0));

            }else {
                username=name.trim();
                sendOne( mapUtil.getJsonMap("suc","设置用户名成功",1));
            }
            return;
        }


        if (checkType(msg.getType())){
            sendOne( mapUtil.getJsonMap("err","禁止发送该类型消息",0));
            return;
        }

        msg.setUsername(username);
        sendGroup(messageUtil.toString(msg));
    }



    public boolean checkType(String type){

        String[] types={"err","login","tip","logout","suc"};

        for(String s:types){


            if (s.equals(type)){

                return true;
            }
        }

        return false;

    }

    public void sendGroup(String message){


        for (String c:webSocketSet.keySet()){
            try {
                webSocketSet.get(c).session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }




    public void sendOne(String message){
        try {
            webSocketSet.get(uid).session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }






}
